package cn.bocweb.spinning.view.adapter;

import android.content.Context;
import android.support.v7.widget.CardView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import cn.bocweb.spinning.R;
import cn.bocweb.spinning.model.Bean.LibListBean;
import cn.bocweb.spinning.util.DeviceUtil;
import cn.bocweb.spinning.view.common.MyAdapter;

/**
 * Created by chenchangxing on 15/12/21.
 */
public class SetQuesWarehouseAdapter extends MyAdapter<LibListBean.DataEntity> {

    int selected = 100;
    ViewHolder holder;


    public SetQuesWarehouseAdapter(Context context, List<LibListBean.DataEntity> list) {
        super(context, list);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.item_set_ques_warehouse, null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.quesName.setText(list.get(position).getTitle());

        if (selected == position) {
            holder.btnWarehouse.setCardBackgroundColor(context.getResources().getColor(R.color.colorPrimary));
        } else {
            holder.btnWarehouse.setCardBackgroundColor(context.getResources().getColor(R.color.bg_set_ques));
        }

        return convertView;
    }

    public int initSelectedById(String id) {
        for (int i = 0; i < list.size(); i++) {
            if (id.equals(list.get(i).getId())) {
                DeviceUtil.Log("id adapter = " + list.get(i).getId());
                selected = i;
            }
        }

        DeviceUtil.Log("select = " + selected);
        return selected;
    }

    /**
     * This class contains all butterknife-injected Views & Layouts from layout file 'item_set_ques_warehouse.xml'
     * for easy to all layout elements.
     *
     * @author ButterKnifeZelezny, plugin for Android Studio by Avast Developers (http://github.com/avast)
     */
    class ViewHolder {
        @Bind(R.id.ques_name)
        TextView quesName;
        @Bind(R.id.bg_warehouse)
        LinearLayout bgWarehouse;
        @Bind(R.id.btn_warehouse)
        CardView btnWarehouse;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }

    public void setSelection(int selected) {
        this.selected = selected;
    }
}
